<?php

namespace app\model;

use app\stateData;
use think\Model;
use think\model\concern\SoftDelete;

class OrderDeal extends Model
{
    /**
     * 订单生成表
     */

    use SoftDelete;
    protected $createTime='create_date';
    protected $deleteTime='delete_date';

    public function getStatusAttr($name): string
    {
        return stateData::$_deal_status[$name];
    }
    public function getOrderStatusAttr($name): array
    {
        return [
            'id'=>$name,
            'text'=>stateData::$_deal_status[$name]
        ];
    }



//electronic_certificate_file_illustrate,
//electronic_certificate_file,
//electronic_certificate_date,
//b_electronic_certificate_file_illustrate,
//b_electronic_certificate_file,
//b_electronic_certificate_date,
//b_reg_electronic_certificate_date,
//electronic_certificate_file_illustrate_name,
//b_electronic_certificate_file_name


    public function getElectronicCertificateFileAttr($name)
    {
        return $name?:'';
    }

    public function getBElectronicCertificateFileIllustrateAttr($name)
    {
        return $name?:'';
    }

    //公示电子证书文件
    public function getElectronicCertificateFileIllustrateAttr($value)
    {
        if (empty($value)) return "";
        $arr = explode(',',$value);
        $value = '';
        foreach($arr as &$v){
            $value .= config('file.url').$v .',';
        }
        return rtrim($value,',');
    }

    //B证电子证书文件
    public function getBElectronicCertificateFileAttr($value)
    {
        if (empty($value)) return "";
        $arr = explode(',',$value);
        $value = '';
        foreach($arr as &$v){
            $value .= config('file.url').$v .',';
        }
        return rtrim($value,',');
    }

    public function getBElectronicCertificateFileNameAttr($name)
    {
        if (empty($name)) return "";
        return $name;
    }
    public function getElectronicCertificateFileIllustrateNameAttr($name)
    {
        if (empty($name)) return "";
        return $name;
    }

    public function getBRegElectronicCertificateDateAttr($name)
    {
        if ($name === '0000-00-00 00:00:00' || empty($name)) return "";
        return date ('Y-m-d',strtotime ($name));
    }
    public function getBElectronicCertificateDateAttr($name)
    {
        if ($name === '0000-00-00 00:00:00' || empty($name)) return "";
        return date ('Y-m-d',strtotime ($name));
    }
    public function getElectronicCertificateDateAttr($name)
    {
        if ($name === '0000-00-00 00:00:00' || empty($name)) return "";
        return date ('Y-m-d',strtotime ($name));
    }

    public function addOrderDataModel($data)
    {
        return self::create($data);
    }

    public function getOrderList()
    {
        return $this->field('
            id,
            order_company_id,
            certificate_code,
            status as order_status,
            create_date as sale_date
        ')->select ()->toArray ();
    }
    public function getOrderCountModel()
    {
        return $this->count ();
    }

    /**
     * 延伸id  分类查询
     * @param $id
     * @param $type
     * @return \app\model\OrderDeal[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
//    public function getWhereFid($id,$type)
//    {
//        return $this->where (['extend_person_id'=>$id,'type'=>$type])->select();
//    }

    /**
     * 证书注册延伸库id 查询单条记录
     * @param $id
     * @return \app\model\OrderDeal|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWhereOid($id)
    {
        return $this->where (['order_company_id'=>$id])->find();
    }

    /**
     * 查询人才姓名已成交数据
     * @param $personName
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonNameModel($personName):array
    {
        return $this->where (['person_name'=>$personName])->select()->toArray ();
    }

    /**
     * 通过证书编码查询成交数量
     * @param $id
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getWhereCertificateCodeCount($id):int
    {
        return $this->where (['certificate_code'=>$id])->count();
    }

    /**
     * 完成成交操作
     * @param $id
     * @param $type
     * @return \app\model\OrderDeal
     */
    public function completeOp($id,$cer)
    {
        return $this->where(['order_company_id'=>$id,'certificate_code'=>$cer])->update (['status'=>-1]);
    }

    /**
     * 修改成交业绩
     * @param $id
     * @param $performance
     * @return \app\model\OrderDeal
     */
    public function upOidPerformance($id,$performance)
    {
        return $this->where (['id'=>$id])->update (['performance'=>$performance]);
    }

    /**
     * 获取id集合数据
     * @param $idArr
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIdArrDataModel($idArr)
    {
        return $this->whereIn ('id',$idArr)->field('
        id,
        order_company_id,
        certificate_code,
        status as order_status,
        create_date,
        type,
        electronic_certificate_file,
        electronic_certificate_file_illustrate,
        electronic_certificate_date,
        b_electronic_certificate_file_illustrate,
        b_electronic_certificate_file,
        b_electronic_certificate_date,
        b_reg_electronic_certificate_date
        ')->select()->toArray ();
    }
    /**
     * 删除成交
     * @param $id
     * @return bool
     */
    public function delOrderModel($id): bool
    {
        return self::destroy($id);
    }

    /**
     * 通过成交id查询成交是否存在
     * @param $id
     * @return array|null
     */
    public function getIdFindModel($id): ?array
    {
        $data =  $this->where (['id'=>$id])->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 通过证书编号查询记录
     * @param $certificate
     * @return array|null
     */
    public function getCertificateIdFindModel($certificate): ?array
    {
        $data =  $this->where (['certificate_code'=>$certificate])->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 获取成交状态
     * @param $id
     * @param $type
     * @return mixed
     */
    public function getOrderIdStatusModel($id,$code){
        return $this->where (['order_company_id'=>$id,'certificate_code'=>$code])->value ('status');
    }

    /**
     * 通过公司补充订单id与证书编号查询是否存在
     * @param $id
     * @param $cer
     * @return array|null
     */
    public function getOrderCompanyIdFindModel($id,$cer): ?array
    {
        $data = $this->where (['order_company_id'=>$id,'certificate_code'=>$cer])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 通过证书编码查询订单是否存在
     * @param $cer
     * @return array|null
     */
    public function getOrderCerFindModel($cer): ?array
    {
        $data = $this->where (['certificate_code'=>$cer])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 获取补充公示资料
     * @param $id
     * @param $cer
     * @return \app\model\OrderDeal|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGszlDataModel($id,$cer)
    {
        $data = $this->where (['order_company_id'=>$id,'certificate_code'=>$cer])
            ->field('
                electronic_certificate_file,
                electronic_certificate_file_illustrate,
                electronic_certificate_file_illustrate_name,
                electronic_certificate_date,
                b_electronic_certificate_file_illustrate,
                b_electronic_certificate_file_name,
                b_electronic_certificate_file,
                b_electronic_certificate_date,
                b_reg_electronic_certificate_date,
                status
            ')
            ->find();

        if (!empty($data)){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 补充公式资料
     * @param $data
     * @return \app\model\OrderDeal
     */
    public function editDataModel($data)
    {

        return $this->where (['certificate_code'=>$data['certificate_code'],'order_company_id'=>$data['id'],'status'=>1])->update ($data);
    }
    public function editIdCerDataModel($id,$cer,$data)
    {
        return $this->where (['order_company_id'=>$id,'certificate_code'=>$cer])->update ($data);
    }

    /**
     * 获取补充公示资料
     * @param $id
     * @param $cer
     * @return \app\model\OrderDeal|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getsuppleInfo($id,$cer)
    {

        return $this->where (['order_company_id'=>$id,'certificate_code'=>$cer])->field('
                electronic_certificate_file_illustrate,
                electronic_certificate_file,
                electronic_certificate_date,
                b_electronic_certificate_file_illustrate,
                b_electronic_certificate_file,
                b_electronic_certificate_date,
                b_reg_electronic_certificate_date,
                electronic_certificate_file_illustrate_name,
                b_electronic_certificate_file_name,
                status as order_status
            ')->find();
    }

    public function getCerComIdFindModel($id,$cer)
    {
        $data = $this->where (['certificate_code'=>$cer,'order_company_id'=>$id])->order ('create_date DESC')->find();
        if ($data){
            return $data['status'];
        }

        return "";
    }

    /**
     * 业绩统计证书注册 按成交status查询  准备弃用
     * @param $id
     * @param $cer
     * @param $status
     * @param $startDate
     * @param $endDate
     * @return array
     */
    public function getCerComIdDateDataModel($id,$cer,$status,$startDate,$endDate)
    {
        if ($status){
            $data = $this->where(['certificate_code'=>$cer,'order_company_id'=>$id,'status'=>$status])->whereBetweenTime ('create_date',$startDate,$endDate)->findOrEmpty ();
        }else{
            $data = $this->where(['certificate_code'=>$cer,'order_company_id'=>$id])->whereBetweenTime ('create_date',$startDate,$endDate)->findOrEmpty ();
        }

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return [];
//        if ($status){
//            $data = $this->where(['certificate_code'=>$cer,'order_company_id'=>$id,'status'=>$status])->whereBetweenTime ('create_date',$startDate,$endDate)->value ('id');
//        }else{
//            $data = $this->where(['certificate_code'=>$cer,'order_company_id'=>$id])->whereBetweenTime ('create_date',$startDate,$endDate)->value ('id');
//        }

//        return $data;

    }

    /**
     * 业绩统计证书注册 按成交status查询
     * @param $startDate
     * @param $endDate
     * @return array
     */
    public function getDateCerIdDataModel($startDate,$endDate,$status='')
    {
        if ($status){
            $data = $this->where(['status'=>$status])->whereBetweenTime ('create_date',$startDate,$endDate)->column ('certificate_code');
        }else{
            return  $this->whereBetweenTime ('create_date',$startDate,$endDate)->column ('certificate_code,status');
        }
        return $data;
    }


    /**
     * 根据订单编号与状态查询是否存在
     */
    public function getCerStatusEmptyModel($cer,$status)
    {
        $data = $this->where (['certificate_code'=>$cer,'status'=>$status])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    public function getCertDateOrB($cer)
    {
        $data = $this->where(['certificate_code' => $cer])
            ->field('electronic_certificate_date,b_reg_electronic_certificate_date')
            ->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }



}